package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyComments;
import cn.wolfcode.trip.base.domain.Zan;
import cn.wolfcode.trip.base.mapper.StrategyCommentsMapper;
import cn.wolfcode.trip.base.mapper.ZanMapper;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentsService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StrategyCommentsServiceImpl implements IStrategyCommentsService {

    @Autowired
    private StrategyCommentsMapper commentsMapper;
    @Autowired
    private ZanMapper zanMapper;

    @Override
    public PageInfo query(StrategyCommentQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<StrategyComments> commentsList = commentsMapper.selectForList(qo);
        for (StrategyComments comments : commentsList) {
            //写死点赞类型为攻略评论点赞,
            Zan zan = new Zan();
            zan.setType(4L);
            if (UserContext.getCurrentUser() != null) {
                zan.setUserId(UserContext.getCurrentUser().getId());
            }
            zan.setTypeId(comments.getId());
            comments.setStatus(zanMapper.getZanByCommentId(zan));

            //获取攻略评论点赞的数量
            Zan zan1 = new Zan();
            zan1.setTypeId(comments.getId());
            zan1.setType(4L);
            comments.setCountZan(zanMapper.getZanNumByTravelId(zan1));
        }
        return new PageInfo<StrategyComments>(commentsList);
    }

    @Override
    public void save(StrategyComments comments) {
        comments.setCommentTime(new Date());
        comments.setCommentUser(UserContext.getCurrentUser());
        commentsMapper.insert(comments);
    }
}
